// On-disk file system format.
// Both the kernel and user programs use this header file.


#define ROOTINO 1  // root i-number
#define BSIZE 512  // block size

// Disk layout:
// [ boot block | super block | log | inode blocks |
//                                          free bit map | data blocks]
//
// mkfs computes the super block and builds an initial file system. The
// super block describes the disk layout:
struct superblock {
  uint size;         // Size of file system image (blocks)
  uint nblocks;      // Number of data blocks
  uint ninodes;      // Number of inodes.
  uint nlog;         // Number of log blocks
  uint logstart;     // Block number of first log block
//  uint inodestart;   // Block number of first inode block
//  uint bmapstart;    // Block number of first free map block
  
  uint nblockgroups; //块组数量

  uint bgstart;      //块组起始
  uint bgsize;       //块组大小

  uint inodeblockperbg; //每组inode块数
  uint bitmapblockperbg;//每组bitmap块数
  uint datablockperbg;  //每组data块数
  uint metablockperbg;  //每组meta块数

  uint inodeperbg;      //每组inode数量
};

#define NDIRECT 12
#define NINDIRECT (BSIZE / sizeof(uint))
#define MAXFILE (NDIRECT + NINDIRECT)

// On-disk inode structure
struct dinode {
  short type;           // File type
  short major;          // Major device number (T_DEV only)
  short minor;          // Minor device number (T_DEV only)
  short nlink;          // Number of links to inode in file system
  uint size;            // Size of file (bytes)
  uint addrs[NDIRECT+1];   // Data block addresses
};

struct fbgstat {
  uint inum;          // Inode number
  uint inodebgroup;   // Block group number that the inode belongs to
  uint ndatablocks;   // Number of data blocks allocated for this file
  uint datablockbgroups[NDIRECT + NINDIRECT]; // Block group number of each data block in this group.
};

// Inodes per block.
#define IPB           (BSIZE / sizeof(struct dinode))

// 通过inode得第几组
#define INODEBG(i,sb)     ((i-1) / sb.inodeperbg)

// 通过inode得该组起点block
#define INODEBGSTART(i,sb)  (sb.bgstart + (INODEBG((i),sb))*sb.bgsize)

// 通过组号得其组起点block
#define BLOCKBGSTART(b,sb)  (sb.bgstart + b*sb.bgsize)

// 得到inode所在的block
#define IBLOCK(i, sb)     (INODEBGSTART((i), sb)) //+ ((i--1) / IPB))

// 通过组号每组的第一个inode
#define FIRSTINODEPERBG(b,sb) ((b)*sb.inodeperbg)+1

// Bitmap bits per block
#define BPB           (BSIZE*8)

// 通过块号得第几组
#define BLOCKBG(b,sb) (((b)-sb.bgstart)/sb.bgsize)

// 通过块号找到其bitmap块
//#define BBLOCK(b, sb) (BLOCKBG(s,sb)*sb.bgsize+sb.inodeblockperbg)
#define BBLOCK(b, sb)  (sb.bgstart+BLOCKBG(b, sb)*sb.bgsize + sb.inodeblockperbg + (((b) - sb.metablockperbg) % sb.bgsize) / BPB)

#define BOFFSET(b,sb)   ((b) - BBLOCK(b,sb) - 1 )
// Directory is a file containing a sequence of dirent structures.
#define DIRSIZ 14

struct dirent {
  ushort inum;
  char name[DIRSIZ];
};

